package pl.edu.agh.xls2html.htmlelements;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Document {

	private ArrayList<Table> tables;
	private Boolean staticMode = false;

	public Document(HSSFWorkbook wb) {
		tables = new ArrayList<Table>();
		int sn = wb.getNumberOfSheets();

		for (int i = 0; i < sn; i++)
			tables.add(new Table(wb.getSheetAt(i), wb, this));
	}

	public int getSheetsNumber() {
		return tables.size();
	}

	public void setStaticMode() {
		staticMode = true;
	}

	public Boolean isStaticMode() {
		return staticMode;
	}

	public String getSheetHTML(int sheet) {
		String content = tables.get(sheet).getHTML();
		return HTMLHelper.makeDocument(content, sheet, !isStaticMode());
	}

	public String getMenuHTML() {
		String content = "";

		for (int i = 0; i < tables.size(); i++)
			content += HTMLHelper.makeSheetHref(i);

		return HTMLHelper.makeDocument(content, -1, false);
	}

	public String getSheetCSS(int sheet) {
		String css = tables.get(sheet).getCSS();
		return css;
	}

	public String getJSResource(String filePath) {
		final String lineSeparator = System.getProperty("line.separator");
		try {

			BufferedReader in = new BufferedReader(new FileReader(filePath));
			StringBuffer out = new StringBuffer();
			String str;
			while ((str = in.readLine()) != null) {
				out.append(str);
				out.append(lineSeparator);
			}

			in.close();
			return out.toString();
		} catch (IOException e) {
			throw new IllegalStateException("Please check resorce.js file");
		}

	}

}
